//侧边栏
$(function(){
    var timelist,timelist1;

    $("body").on("mouseover mouseout", ".admin-left-list",function(event){
        if(event.type == "mouseover"){
            $(this).addClass('current');
            clearTimeout(timelist);
            var listdd = $(this).find("dd");
            if(listdd.is(":hidden")){
                timelist1 = setTimeout(function(){
                    $(".admin-left-list dd").hide();
                    listdd.show();
                },100);
            }
        }else if(event.type == "mouseout"){
            $(this).removeClass('current');
            clearTimeout(timelist1);
            var listdd = $(this).find("dd");
            timelist = setTimeout(function(){
                listdd.hide();
            },100);
        }
    });

});

var resize = function(){
    var winHeight = $(window).height() -33;
    var minHeight = $(".son51-cont-right").height();
    if (minHeight >= winHeight) {
        $(".son51-cont-left").css("height",minHeight);
    } else{
        $(".son51-cont-left").css("min-height",winHeight);
    };
};

$(window).resize(function(){
    resize();
});


//自适应高度
$$.templateload(function(){
    resize();
});



var pagenation = function(container, pagerow,pagenum,rowcount,pagecount,callback){

    var row = $("<div>").addClass("row");
    var col5 = $("<div>").addClass("col-md-5 col-sm-5");
    var col7 = $("<div>").addClass("col-md-7 col-sm-7");
    var show_page = $("<div>").addClass("dataTables_info pagemsg").append("每页显示"+pagerow+"条，当前" + pagenum + "/" +pagecount + "页 共" +rowcount + "条");

    var ul = $("<ul>").addClass("pagination pull-right");
    var laquo = $("<li>");
    var raquo = $("<li>");
    var list = $("<li>");

    var laquo_att = $("<a>").attr("id", "pre-page").append("&laquo;");
    laquo.append(laquo_att);

    var raquo_att = $("<a>").attr("id", "next-page").append("&raquo;");
    raquo.append(raquo_att);

    var articles = $("<a>").addClass("page-num");

    row.append(col5).append(col7);
    col5.append(show_page);
    col7.append(ul);
    ul.append(laquo).append(list).append(raquo);

    for (var i=0;i<pagecount;i++){
        var a = $("<a>").addClass("page-num").text(i+1);
        var li = $("<li>").append(a);
        raquo.before(li);
        li.on("click", function(){
            var num = Number($(this).find("a").text());
            if(callback){
                callback(num);
            };
        });
    }


    $(container).html("");
    $(container).append(row);  //over
    //
    $(".page-num").eq(pagenum-1).parent().addClass("active");

    if (pagenum == 1){
        $("#pre-page").parent().addClass("disabled");
    }else{
        $("#pre-page").parent().removeClass("disabled");
    };
    if (pagenum == pagecount){
        $("#next-page").parent().addClass("disabled");
    }else{
        $("#next-page").parent().removeClass("disabled");
    };
    
    if ((pagenum - 1) <= 0) {
        $("#pre-page").attr("href", "javascript:void(0)");
    };
    if ((pagenum + 1) >= pagecount) {
        $("#next-page").attr("href", "javascript:void(0)");
    }

    $("body").on("click", ".page-num", function(){
        $(".page-num").parent().removeClass("active");
        $(this).parent().addClass("active");
        var cli_page = $(this).text();
        if (cli_page == 1){
            $("#pre-page").parent().addClass("disabled");
        }else{
            $("#pre-page").parent().removeClass("disabled");
        };
        if (cli_page == pagecount){
            $("#next-page").parent().addClass("disabled");
        }else{
            $("#next-page").parent().removeClass("disabled");
        };
    });
    $("body").on("click", "#pre-page", function(){
        if ((pagenum - 1) <= 0) {

        }else{
            var num = Number(pagenum-1);
            if(callback){
                callback(num);
            };
            window.location.reload()
        };
    });
    $("body").on("click", "#next-page", function(){
        if ((pagenum + 1) >= pagecount) {
            
        } else{
            var num = Number(pagenum +1);
            if(callback){
                callback(num);
            };
            window.location.reload()
        };
    });
};



function onSearch(obj){//js函数开始  
    setTimeout(function(){//因为是即时查询，需要用setTimeout进行延迟，让值写入到input内，再读取  
        var storeId = document.getElementById('store');//获取table的id标识  
        var rowsLength = storeId.rows.length;//表格总共有多少行  
        var key = obj.value;//获取输入框的值  
  
        var searchCol = 2;//要搜索的哪一列，这里是第一列，从0开始数起  
  
        for(var i=1;i<rowsLength;i++){//按表的行数进行循环，本例第一行是标题，所以i=1，从第二行开始筛选（从0数起）  
            var searchText = storeId.rows[i].cells[searchCol].innerHTML;//取得table行，列的值  
  
            if(searchText.match(key)){//用match函数进行筛选，如果input的值，即变量 key的值为空，返回的是ture，  
                storeId.rows[i].style.display='';//显示行操作，  
            }else{  
                storeId.rows[i].style.display='none';//隐藏行操作  
            }  
        }  
    },200);//200为延时时间  
};




// 
// 


!function(win, lib) {
    var timer,
        doc     = win.document,
        docElem = doc.documentElement,

        vpMeta   = doc.querySelector('meta[name="viewport"]'),
        flexMeta = doc.querySelector('meta[name="flexible"]'),
 
        dpr   = 0,
        scale = 0,
 
        flexible = lib.flexible || (lib.flexible = {});
 
    // 设置了 viewport meta
    if (vpMeta) {
 
        
        var initial = vpMeta.getAttribute("content").match(/initial\-scale=([\d\.]+)/);
 
        if (initial) {
            scale = parseFloat(initial[1]); 
            dpr = parseInt(1 / scale);      
        }
 
    }
    // 设置了 flexible Meta
    else if (flexMeta) {
        var flexMetaContent = flexMeta.getAttribute("content");
        if (flexMetaContent) {
 
            var initial = flexMetaContent.match(/initial\-dpr=([\d\.]+)/),
                maximum = flexMetaContent.match(/maximum\-dpr=([\d\.]+)/);
 
            if (initial) {
                dpr = parseFloat(initial[1]);
                scale = parseFloat((1 / dpr).toFixed(2));
            }
 
            if (maximum) {
                dpr = parseFloat(maximum[1]);
                scale = parseFloat((1 / dpr).toFixed(2));
            }
        }
    }
 
    // viewport 或 flexible
    // meta 均未设置
    if (!dpr && !scale) {
        // QST
        // 这里的 第一句有什么用 ?
        // 和 Android 有毛关系 ?
        var u = (win.navigator.appVersion.match(/android/gi), win.navigator.appVersion.match(/iphone/gi)),
            _dpr = win.devicePixelRatio;
 
        // 所以这里似乎是将所有 Android 设备都设置为 1 了
        dpr = u ? ( (_dpr >= 3 && (!dpr || dpr >= 3))
                        ? 3
                        : (_dpr >= 2 && (!dpr || dpr >= 2))
                            ? 2
                            : 1
                  )
                : 1;
 
        scale = 1 / dpr;
    }
 
    docElem.setAttribute("data-dpr", dpr);
 
    // 插入 viewport meta
    if (!vpMeta) {
        vpMeta = doc.createElement("meta");
         
        vpMeta.setAttribute("name", "viewport");
        vpMeta.setAttribute("content",
            "initial-scale=" + scale + ", maximum-scale=" + scale + ", minimum-scale=" + scale + ", user-scalable=no");
 
        if (docElem.firstElementChild) {
            docElem.firstElementChild.appendChild(vpMeta)
        } else {
            var div = doc.createElement("div");
            div.appendChild(vpMeta);
            doc.write(div.innerHTML);
        }
    }
 
    function setFontSize() {
        var winWidth = docElem.getBoundingClientRect().width;
 
        if (winWidth / dpr > 540) {
            (winWidth = 540 * dpr);
        }
 
        // 根节点 fontSize 根据宽度决定
        var baseSize = winWidth / 10;
 
        docElem.style.fontSize = baseSize + "px";
        flexible.rem = win.rem = baseSize;
    }
 
    // 调整窗口时重置
    win.addEventListener("resize", function() {
        clearTimeout(timer);
        timer = setTimeout(setFontSize, 300);
    }, false);
 
     
    // 这一段是我自己加的
    // orientationchange 时也需要重算下吧
    win.addEventListener("orientationchange", function() {
        clearTimeout(timer);
        timer = setTimeout(setFontSize, 300);
    }, false);
 
 
    // pageshow
    // keyword: 倒退 缓存相关
    win.addEventListener("pageshow", function(e) {
        if (e.persisted) {
            clearTimeout(timer);
            timer = setTimeout(setFontSize, 300);
        }
    }, false);
 
    // 设置基准字体
    if ("complete" === doc.readyState) {
        doc.body.style.fontSize = 12 * dpr + "px";
    } else {
        doc.addEventListener("DOMContentLoaded", function() {
            doc.body.style.fontSize = 12 * dpr + "px";
        }, false);
    }
  
    setFontSize();
 
    flexible.dpr = win.dpr = dpr;
 
    flexible.refreshRem = setFontSize;
 
    flexible.rem2px = function(d) {
        var c = parseFloat(d) * this.rem;
        if ("string" == typeof d && d.match(/rem$/)) {
            c += "px";
        }
        return c;
    };
 
    flexible.px2rem = function(d) {
        var c = parseFloat(d) / this.rem;
         
        if ("string" == typeof d && d.match(/px$/)) {
            c += "rem";
        }
        return c;
    }
}(window, window.lib || (window.lib = {}));


function goTop(acceleration, time) {
    acceleration = acceleration || 0.1;
    time = time || 16;
    var x1 = 0;
    var y1 = 0;
    var x2 = 0;
    var y2 = 0;
    var x3 = 0;
    var y3 = 0;
    if (document.documentElement) {
    x1 = document.documentElement.scrollLeft || 0;
    y1 = document.documentElement.scrollTop || 0;
    }
    if (document.body) {
    x2 = document.body.scrollLeft || 0;
    y2 = document.body.scrollTop || 0;
    }
    var x3 = window.scrollX || 0;
    var y3 = window.scrollY || 0;
    // 滚动条到页面顶部的水平距离
    var x = Math.max(x1, Math.max(x2, x3));
    // 滚动条到页面顶部的垂直距离
    var y = Math.max(y1, Math.max(y2, y3));
    // 滚动距离 = 目前距离 / 速度, 因为距离原来越小, 速度是大于 1 的数, 所以滚动距离会越来越小
    var speed = 1 + acceleration;
    window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
    // 如果距离不为零, 继续调用迭代本函数
    if (x > 0 || y > 0) {
    var invokeFunction = "goTop(" + acceleration + ", " + time + ")";
    window.setTimeout(invokeFunction, time);
    }
  }
    $("#backtop").on("click", function() {
        goTop();
    });
    $(window).scroll(function(){
      var htmlTop = $(document).scrollTop();
      if( htmlTop > 0){
        $("#backtop").fadeIn(); 
      }else{
        $("#backtop").fadeOut();
      }
    });